package com.kt.controller;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kt.po.User;
import com.kt.service.UserService;

@RestController
@CrossOrigin
@RequestMapping("/userController")
public class UserController {
    @Autowired
    private UserService userService;

    // /userController/addUser?username=测试&password=123456&gxqm=哈哈
    @RequestMapping("/addUser")
    public Map<String, Object> addUser(String username, String password, String gxqm) {

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setGxqm(gxqm);
        user.setDate(new Date());

        int res = userService.addUser(user);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("responseMsg", res);
        return map;
    }

    // /userController/addUserJpa?username=测试&password=123456&gxqm=哈哈
    @RequestMapping("/addUserJpa")
    public Map<String, Object> addUserJpa(User user) {
        user.setDate(new Date());
        User newuser = userService.addUserJpa(user);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("responseMsg", 1);
        map.put("user", newuser);
        return map;
    }

    // /userController/deleteUser?username=测试
    @RequestMapping("/deleteUser")
    public Map<String, Object> deleteUser(String username) {
        int res = userService.deleteUserByUsername(username);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("responseMsg", res);
        return map;
    }

    // /userController/findAllUser
    @RequestMapping("/findAllUser")
    public List<Map<String, Object>> findAllUser() {
        return userService.findAllUser();
    }

    // /userController/findAllUserJpa
    @RequestMapping("/findAllUserJpa")
    public List<User> findAllUserJpa() {
        return userService.findAllUserJpa();
    }

    // /userController/addUserMap?username=测试&password=123456&gxqm=哈哈
    @RequestMapping("/addUserMap")
    public int addUserMap(User user) {
        user.setDate(new Date());
        return userService.addUserMap(user);
    }

    // /userController/deleteUserMap?id=
    @RequestMapping("/deleteUserMap")
    public int deleteUserMap(int id) {
        return userService.deleteUserMap(id);
    }

    // /userController/findUserByUsernameJpa?username=测试
    @RequestMapping("/findUserByUsernameJpa")
    public List<User> findUserByUsernameJpa(String username) {
        return userService.findUserByUsernameJpa(username);
    }

    // /userController/findUserByNameMap?username=测试
    @RequestMapping("/findUserByNameMap")
    public List<User> findUserByNameMap(String username) {
        return userService.findUserByNameMap(username);
    }

    // /userController/findUserPageMap?page=1&size=10
    @RequestMapping("/findUserPageMap")
    public List<User> findUserPageMap(int page, int size) {
        return userService.findUserPageMap(page, size);
    }

    // /userController/selectUserWithParam?username=大大
    @RequestMapping("/selectUserWithParam")
    public Map<String, Object> selectUserWithParam(@RequestParam Map<String, Object> map) {
        return userService.selectUserWithParam(map);
    }

    @RequestMapping("/variable/{test}")
    public Map<String, Object> test(@PathVariable String test) {
        Map<String,Object> map= new HashMap<>();
        map.put("variable",test);
        return map;
    }
}
